package com.bm.beautifulmouse.effect;

import com.bm.beautifulmouse.model.EffectConfig;
import javafx.animation.*;
import javafx.scene.layout.Pane;
import javafx.scene.shape.SVGPath;
import javafx.util.Duration;
import java.util.Random;

/**
 * 猫爪特效
 */
public class PawEffect implements Effect {
    private final Random random = new Random();

    @Override
    public void generate(Pane container, double x, double y, EffectConfig config) {
        // 创建主爪印
        createMainPaw(container, x, y, config);
        
        // 创建周围的小爪印
        createSurroundingPaws(container, x, y, config);
    }

    private void createMainPaw(Pane container, double x, double y, EffectConfig config) {
        SVGPath mainPaw = createPaw(config.getSize() * 1.5);
        mainPaw.setFill(config.getColor());
        mainPaw.setOpacity(0.8);

        double startX = x - (mainPaw.getBoundsInLocal().getWidth() / 2);
        double startY = y - (mainPaw.getBoundsInLocal().getHeight() / 2);
        mainPaw.setLayoutX(startX);
        mainPaw.setLayoutY(startY);

        // 主爪印动画
        Timeline mainTimeline = new Timeline(
            new KeyFrame(Duration.ZERO,
                new KeyValue(mainPaw.scaleXProperty(), 0.2),
                new KeyValue(mainPaw.scaleYProperty(), 0.2),
                new KeyValue(mainPaw.opacityProperty(), 0.0)
            ),
            new KeyFrame(Duration.seconds(0.3),
                new KeyValue(mainPaw.scaleXProperty(), 1.2),
                new KeyValue(mainPaw.scaleYProperty(), 1.2),
                new KeyValue(mainPaw.opacityProperty(), 0.8)
            ),
            new KeyFrame(Duration.seconds(1.0),
                new KeyValue(mainPaw.scaleXProperty(), 1.0),
                new KeyValue(mainPaw.scaleYProperty(), 1.0),
                new KeyValue(mainPaw.opacityProperty(), 0.0)
            )
        );

        container.getChildren().add(mainPaw);
        mainTimeline.setOnFinished(e -> container.getChildren().remove(mainPaw));
        mainTimeline.play();
    }

    private void createSurroundingPaws(Pane container, double x, double y, EffectConfig config) {
        // 创建5个爪印围绕主爪印
        double[] angles = {-30, 30, 150, -150, 90};  // 爪印分布角度
        
        for (int i = 0; i < angles.length; i++) {
            SVGPath paw = createPaw(config.getSize() * 0.8);
            paw.setFill(config.getColor());
            
            double startX = x - (paw.getBoundsInLocal().getWidth() / 2);
            double startY = y - (paw.getBoundsInLocal().getHeight() / 2);
            paw.setLayoutX(startX);
            paw.setLayoutY(startY);
            
            double angle = angles[i] + random.nextDouble() * 20 - 10;
            double distance = (40 + random.nextDouble() * 20) * config.getRange();
            double duration = 1.2 / config.getSpeed();
            
            double targetX = startX + Math.cos(Math.toRadians(angle)) * distance;
            double targetY = startY + Math.sin(Math.toRadians(angle)) * distance;
            
            // 添加旋转动画
            double rotation = angle + random.nextDouble() * 45 - 22.5;
            
            Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO,
                    new KeyValue(paw.layoutXProperty(), startX),
                    new KeyValue(paw.layoutYProperty(), startY),
                    new KeyValue(paw.scaleXProperty(), 0.2),
                    new KeyValue(paw.scaleYProperty(), 0.2),
                    new KeyValue(paw.rotateProperty(), 0),
                    new KeyValue(paw.opacityProperty(), 1.0)
                ),
                new KeyFrame(Duration.seconds(duration * 0.4),
                    new KeyValue(paw.scaleXProperty(), 1.0),
                    new KeyValue(paw.scaleYProperty(), 1.0),
                    new KeyValue(paw.rotateProperty(), rotation * 0.6)
                ),
                new KeyFrame(Duration.seconds(duration),
                    new KeyValue(paw.layoutXProperty(), targetX),
                    new KeyValue(paw.layoutYProperty(), targetY),
                    new KeyValue(paw.scaleXProperty(), 0.6),
                    new KeyValue(paw.scaleYProperty(), 0.6),
                    new KeyValue(paw.rotateProperty(), rotation),
                    new KeyValue(paw.opacityProperty(), 0.0)
                )
            );

            container.getChildren().add(paw);
            timeline.setOnFinished(e -> container.getChildren().remove(paw));
            timeline.play();
        }
    }

    private SVGPath createPaw(double size) {
        SVGPath paw = new SVGPath();
        // 优化的猫爪SVG路径，更加圆润和逼真
        paw.setContent(
            "M 10 2 " +
            "C 4.5 2 0 6.5 0 12 " +
            "C 0 17.5 4.5 22 10 22 " +
            "C 15.5 22 20 17.5 20 12 " +
            "C 20 6.5 15.5 2 10 2 " +
            "M 5 -6 " +
            "C 2.5 -6 0.5 -4 0.5 -1.5 " +
            "C 0.5 1 2.5 3 5 3 " +
            "C 7.5 3 9.5 1 9.5 -1.5 " +
            "C 9.5 -4 7.5 -6 5 -6 " +
            "M 15 -6 " +
            "C 12.5 -6 10.5 -4 10.5 -1.5 " +
            "C 10.5 1 12.5 3 15 3 " +
            "C 17.5 3 19.5 1 19.5 -1.5 " +
            "C 19.5 -4 17.5 -6 15 -6 " +
            "M 3 -1 " +
            "C 0.5 -1 -1.5 1 -1.5 3.5 " +
            "C -1.5 6 0.5 8 3 8 " +
            "C 5.5 8 7.5 6 7.5 3.5 " +
            "C 7.5 1 5.5 -1 3 -1 " +
            "M 17 -1 " +
            "C 14.5 -1 12.5 1 12.5 3.5 " +
            "C 12.5 6 14.5 8 17 8 " +
            "C 19.5 8 21.5 6 21.5 3.5 " +
            "C 21.5 1 19.5 -1 17 -1"
        );

        double scale = size;
        paw.setScaleX(scale);
        paw.setScaleY(scale);

        return paw;
    }
}